package gui.specialGUIs;

import gui.designComponents.TMTComboBox;
import gui.designComponents.TMTDefaultFormBuilder;
import gui.designComponents.TMTNormalButton;
import gui.designComponents.TMTPanel;
import gui.designComponents.TMTTextField;

import java.awt.BorderLayout;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.ResourceBundle;

import javax.swing.JTextField;

import util.Auth;
import app.Controller;
import app.Model;
import bundles.BundleLoader;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import definitions.Commands;
import definitions.Constants;
import entities.Member;

/**
 * Panel to show and edit a single Member
 * 
 * @author MK
 * 
 */
public class MemberSinglePanel extends TMTPanel {

	private TMTTextField nameFirstField;
	private TMTTextField nameLastField;
	private TMTTextField yearField;
	private TMTComboBox monthBox;
	private TMTComboBox dayBox;
	private TMTTextField streetField;
	private TMTTextField housenumberField;
	private TMTTextField postalField;
	private TMTTextField cityField;
	private TMTTextField phoneHomeField;
	private TMTTextField phoneMobileField;
	private TMTTextField emailField;
	private TMTComboBox stateBox;
	private TMTComboBox positionBox;
	private TMTComboBox roleBox;
	private MemberSingleButtonPanel buttonPanel;
	
	private Model model;
	private Controller controller;

	public MemberSinglePanel(Model model, Controller controller) {

		this.model = model;
		this.controller = controller;

		ResourceBundle resBundle = BundleLoader.getResourceBundle();

		setLayout(new BorderLayout());

		monthBox = new TMTComboBox(model.getMonthValues());
		dayBox = new TMTComboBox(model.getDaysOfMonthValues());
		stateBox = new TMTComboBox(model.getStateValues());
		positionBox = new TMTComboBox(model.getPositionValues());
		roleBox = new TMTComboBox(model.getRoleValues());

		nameFirstField = new TMTTextField();
		nameLastField = new TMTTextField();
		yearField = new TMTTextField();
		streetField = new TMTTextField();
		housenumberField = new TMTTextField();
		postalField = new TMTTextField();
		cityField = new TMTTextField();
		phoneHomeField = new TMTTextField();
		phoneMobileField = new TMTTextField();
		emailField = new TMTTextField();

		FormLayout layout = new FormLayout(
				"right:max(40dlu;p), 4dlu, 100dlu, 7dlu, "
						+ "right:max(40dlu;p), 4dlu, 100dlu, 7dlu, "
						+ "right:max(40dlu;p), 4dlu, 80dlu", 
						"");

		DefaultFormBuilder builder = TMTDefaultFormBuilder.getDFB(layout);

		builder.appendSeparator(resBundle.getString("name"));
		builder.append(resBundle.getString("nameFirst"), nameFirstField);
		builder.append(resBundle.getString("nameLast"), nameLastField);

		builder.nextLine();
		builder.appendSeparator(resBundle.getString("dateOfBirth"));

		builder.append(resBundle.getString("year"), yearField);
		builder.append(resBundle.getString("month"), monthBox);
		builder.append(resBundle.getString("day"), dayBox);

		builder.nextLine();

		builder.appendSeparator(resBundle.getString("address"));
		builder.append(resBundle.getString("street"), streetField);
		builder.append(resBundle.getString("housenumber"), housenumberField);
		builder.nextLine();
		builder.append(resBundle.getString("postal"), postalField);
		builder.append(resBundle.getString("city"), cityField);
		builder.nextLine();
		builder.appendSeparator(resBundle.getString("contact"));
		builder.append(resBundle.getString("phoneHome"), phoneHomeField);
		builder.append(resBundle.getString("phoneMobile"), phoneMobileField);
		builder.nextLine();
		builder.append(resBundle.getString("email"), emailField);
		builder.nextLine();

		builder.appendSeparator(resBundle.getString("specific"));
		builder.nextLine();
		builder.append(resBundle.getString("state"), stateBox);
		builder.nextLine();
		builder.append(resBundle.getString("position"), positionBox);
		builder.nextLine();
		builder.append(resBundle.getString("role"), roleBox);
		builder.nextLine();

		TMTNormalButton accountButton = new TMTNormalButton("ACCOUNT");
		accountButton.setActionCommand(Commands.SHOW_MEMBER_ACCOUNT);
		accountButton.addActionListener(controller);

		builder.appendSeparator(resBundle.getString("administrateAccount"));
		builder.append("", accountButton);

		add(builder.getPanel(), BorderLayout.CENTER);

		this.buttonPanel = new MemberSingleButtonPanel(model, controller);

		add(buttonPanel, BorderLayout.SOUTH);
	}

	public void update() {

		Member member = model.getSelectedMember();

		if (member == null) {

			nameFirstField.setEditable(true);
			nameLastField.setEditable(true);
			yearField.setEditable(true);
			monthBox.setEnabled(true);
			dayBox.setEnabled(true);

			monthBox.setSelectedIndex(0);
			dayBox.setSelectedIndex(0);

			nameFirstField.setText("");
			nameLastField.setText("");
			yearField.setText("");
			streetField.setText("");
			housenumberField.setText("");
			postalField.setText("");
			cityField.setText("");
			phoneHomeField.setText("");
			phoneMobileField.setText("");
			emailField.setText("");

			roleBox.setSelectedIndex(0);
			positionBox.setSelectedIndex(0);
			stateBox.setSelectedIndex(0);

			streetField.setEditable(true);
			housenumberField.setEditable(true);
			postalField.setEditable(true);
			cityField.setEditable(true);
			phoneHomeField.setEditable(true);
			phoneMobileField.setEditable(true);
			emailField.setEditable(true);
			positionBox.setEnabled(true);
			stateBox.setEnabled(true);
			roleBox.setEnabled(true);

		} else {

			nameFirstField.setText(member.getNameFirst());
			nameLastField.setText(member.getNameLast());

			Long dateOfBirth = member.getDateOfBirth();
			Calendar calendar = GregorianCalendar.getInstance();
			Date dateTime = new Date(dateOfBirth);
			calendar.setTime(dateTime);

			yearField.setText("" + calendar.get(Calendar.YEAR));

			monthBox.setSelectedIndex(calendar.get(Calendar.MONTH));
			dayBox.setSelectedIndex(calendar.get(Calendar.DAY_OF_MONTH) - 1);

			streetField.setText(member.getAddress().getStreet());
			housenumberField.setText("" + member.getAddress().getHousenumber());
			postalField.setText("" + member.getAddress().getPostal());
			cityField.setText(member.getAddress().getCity());
			phoneHomeField.setText(member.getPhoneHome());
			phoneMobileField.setText(member.getPhoneMobile());
			emailField.setText(member.geteMail());

			roleBox.setSelectedIndex(Constants.getConstantAsInt(member
					.getRole()));
			positionBox.setSelectedIndex(Constants.getConstantAsInt(member
					.getPosition()));
			stateBox.setSelectedIndex(Constants.getConstantAsInt(member
					.getState()));

			nameFirstField.setEditable(false);
			nameLastField.setEditable(false);
			yearField.setEditable(false);
			monthBox.setEnabled(false);
			dayBox.setEnabled(false);

			Boolean authorizated = Auth.getAuthorization(member, model);

			if (!authorizated) {
				streetField.setEditable(false);
				housenumberField.setEditable(false);
				postalField.setEditable(false);
				cityField.setEditable(false);
				phoneHomeField.setEditable(false);
				phoneMobileField.setEditable(false);
				emailField.setEditable(false);
				positionBox.setEnabled(false);
				stateBox.setEnabled(false);
				roleBox.setEnabled(false);
			}

			else {
				streetField.setEditable(true);
				housenumberField.setEditable(true);
				postalField.setEditable(true);
				cityField.setEditable(true);
				phoneHomeField.setEditable(true);
				phoneMobileField.setEditable(true);
				emailField.setEditable(true);
				positionBox.setEnabled(true);
				stateBox.setEnabled(true);

				if (member.getRole().equals(Constants.ROLE_COACH)) {
					roleBox.setEnabled(true);
				}
			}
		}

		remove(buttonPanel);
		this.buttonPanel = new MemberSingleButtonPanel(model, controller);
		add(buttonPanel, BorderLayout.SOUTH);
	}

	public JTextField getNameFirstField() {
		return nameFirstField;
	}

	public JTextField getNameLastField() {
		return nameLastField;
	}

	public TMTTextField getYearField() {
		return yearField;
	}

	public TMTComboBox getMonthBox() {
		return monthBox;
	}

	public TMTComboBox getDayBox() {
		return dayBox;
	}

	public TMTTextField getStreetField() {
		return streetField;
	}

	public TMTTextField getHousenumberField() {
		return housenumberField;
	}

	public TMTTextField getPostalField() {
		return postalField;
	}

	public TMTTextField getCityField() {
		return cityField;
	}

	public TMTTextField getPhoneHomeField() {
		return phoneHomeField;
	}

	public TMTTextField getPhoneMobileField() {
		return phoneMobileField;
	}

	public TMTTextField getEmailField() {
		return emailField;
	}

	public TMTComboBox getStateBox() {
		return stateBox;
	}

	public TMTComboBox getPositionBox() {
		return positionBox;
	}

	public TMTComboBox getRoleBox() {
		return roleBox;
	}

	public Model getModel() {
		return model;
	}

	public Controller getController() {
		return controller;
	}
}

class MemberSingleButtonPanel extends TMTPanel {

	public MemberSingleButtonPanel(Model model, Controller controller) {

		Member member = model.getSelectedMember();

		ResourceBundle resBundle = BundleLoader.getResourceBundle();

		TMTNormalButton backButton = new TMTNormalButton(resBundle
				.getString("back"));
		backButton.setActionCommand(Commands.GO_BACK);
		backButton.addActionListener(controller);
		add(backButton);

		Boolean authorizated = Auth.getAuthorization(member, model);

		if (authorizated) {

			TMTNormalButton submitButton = new TMTNormalButton(resBundle
					.getString("submit"));
			submitButton.setActionCommand(Commands.MEMBER_UPDATE);
			submitButton.addActionListener(controller);
			add(submitButton);

			if (member != null) {
				TMTNormalButton deleteButton = new TMTNormalButton(resBundle
						.getString("delete"));
				deleteButton.setActionCommand(Commands.MEMBER_DELETE);
				deleteButton.addActionListener(controller);
				add(deleteButton);
			}
		}
	}
}